package com.rachel.studyapp.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.rachel.commonsdk.glide.GlideManager;
import com.rachel.studyapp.R;
import com.rachel.studyapp.entries.HomePagerBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhengshaorui on 2017/8/24.
 */

public class BasePagerAdapter extends PagerAdapter{
    public static final int RANG_COUNT = 500;
    private List<HomePagerBean> mList = new ArrayList<>();
    private Context mContext;
    private LayoutInflater mLayoutInflater;

    public BasePagerAdapter(Context context,List<HomePagerBean> mList) {
        this.mList = mList;
        mContext = context;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return mList.size()+RANG_COUNT;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        HomePagerBean bean = mList.get(position % mList.size());
        View rootView = mLayoutInflater.inflate(R.layout.home_item_list_type_viewpager,null);

        TextView titleView = (TextView) rootView.findViewById(R.id.title_view);
        TextView infoView = (TextView) rootView.findViewById(R.id.info_view);
        TextView gonggaoView = (TextView) rootView.findViewById(R.id.gonggao_view);
        TextView saleView = (TextView) rootView.findViewById(R.id.sale_num_view);
        ImageView[] imageViews = new ImageView[3];
        imageViews[0] = (ImageView) rootView.findViewById(R.id.image_one);
        imageViews[1] = (ImageView) rootView.findViewById(R.id.image_two);
        imageViews[2] = (ImageView) rootView.findViewById(R.id.image_three);
        /*rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, CourseDetailActivity.class);
                intent.putExtra(CourseDetailActivity.COURSE_ID, value.adid);
                mContext.startActivity(intent);
            }
        });*/

        titleView.setText(bean.getTitle());
        infoView.setText(bean.getPrice());
        gonggaoView.setText(bean.getText());
        saleView.setText(bean.getText());
        for (int i = 0; i < imageViews.length; i++) {
           // mImageLoader.displayImage(imageViews[i], value.url.get(i));
            new GlideManager.Builder()
                    .setContext(mContext)
                    .setTargetSource(bean.getUrl().get(i))
                    .setImageView(imageViews[i])
                    .builder();
        }
        container.addView(rootView, 0);

        return rootView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
}
